package net.lovesrc.allin.biz.gogohome;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import net.lovesrc.allin.biz.BaseService;
import net.lovesrc.allin.utils.MyRandomUtils;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeService;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeWrapper;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class GogoHomeRegister extends BaseService {

    @Resource(name = "noNameMobileVerifyCodeService")
    private MobileVerifyCodeService mobileVerifyCodeService;

    public String newRegister() {


        String mobile;
        try {
            mobile = doGetNewMobile();
        } catch (Exception e) {
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException ex) {

            }
            throw e;
        }

// 测试提交
        String key;
        try {
            JSONObject jsonObject = Requests.get("http://cr.dongweicom.com/api/verify_code").execute().asJson();
            key = jsonObject.getJSONObject("data").getString("key");
        } catch (Exception e) {
            throw BizException.messageOf("注册校验失败");
        }

        Requests.post("http://cr.dongweicom.com/api/register/verify")
                .contentType("application/json")
                .param("phone", mobile)
                .param("key", key)
                .param("code", "")
                .param("type", "register")
                .execute();
        String verifyCode = getMobileVerifyCodeWrapper().getVerifyCodeFromPhone(mobile);

        String nikeName = MyRandomUtils.randomNikeName(true, 2);
        String password = MyRandomUtils.randomPassword();

        int i = random.nextInt(arrays.size());
        MainUser mainUser = arrays.get(i);

        JSONObject regResJson = Requests.post("http://cr.dongweicom.com/api/register")
                .param("account", mobile)
                .param("nickname", nikeName)
                .param("password", password)
                .param("captcha", verifyCode)
                .param("spread", mainUser.getId())
                .execute()
                .asJson();

        if (regResJson != null && "注册成功,请登录".equals(regResJson.getString("msg"))) {
            JSONObject newRow = new JSONObject()
                    .fluentPut("username", mobile)
                    .fluentPut("password", password)
                    .fluentPut("number", "0")
                    .fluentPut("invitationCode", mainUser.getId())
                    .fluentPut("fatherInvitationCode", mainUser.getId())
                    .fluentPut("sign", mainUser.getPhone())
                    .fluentPut("registerTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                    .fluentPut("signTime", "0");
            insertRow(jdbcTemplate, "MGT_USER_INFO", newRow);

            try {
                HomeAccountQueue.queue.put(newRow);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return mobile;
    }

    private String doGetNewMobile() {
        for (; ; ) {
             String mobile = getMobileVerifyCodeWrapper().getPhone(null,"2");
            if (jdbcTemplate.queryForObject("select count(1) from MGT_USER_INFO where username = ?", long.class, mobile) > 0) {
                getMobileVerifyCodeWrapper().addBlack(mobile);
                continue;
            }
            return mobile;
        }
    }

    private MobileVerifyCodeWrapper getMobileVerifyCodeWrapper() {
        return new MobileVerifyCodeWrapper(mobileVerifyCodeService, "14761");
    }


//    public List<MainUser> arrays = Arrays.asList(new MainUser("18107965722", "1042"), new MainUser("18107067457", "1043"));
    public List<MainUser> arrays = Arrays.asList(new MainUser("15950162365", "1034"));

    Random random = new Random();

    public static class MainUser {
        String phone;
        String id;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public MainUser(String phone, String id) {

            this.phone = phone;
            this.id = id;
        }
    }
}
